<template>
    <h2 align="center">增加房价计划</h2>
    <table border="1" align="center">
        <tr>
            <td>房价名称</td>
            <td><input type="text" v-model="state.roomPriceName"></td>
        </tr>
        <tr>
            <td>房量数量</td>
            <td>
                <input type="text" v-model="state.roomPriceNum">
            </td>
        </tr>
        <tr>
            <td>设置价格</td>
            <td><input type="text" v-model="state.price"></td>
        </tr>
        <tr>
            <td>房型ID</td>
            <td>
                <select v-model="state.roomTypeId">
                    <option value="">请选择</option>
                    <option :value="item.id" v-for="item in roomtypelist">{{item.roomTypeName}}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>门店ID</td>
            <td>
                <select v-model="state.storeId">
                    <option value="">请选择</option>
                    <option :value="item.id" v-for="item in storelist">{{ item.storeName }}</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>价格ID</td>
            <td><input type="text" v-model="state.priceId"></td>
        </tr>
        <tr>
            <td>早餐数量</td>
            <td><input type="text"  v-model="state.breakfast"></td>
        </tr>
        <tr>
            <td>人数数量</td>
            <td><input type="text"  v-model="state.people"></td>
        </tr>
        <tr>
            <td>开始时间</td>
            <td><input type="date"  v-model="state.sdate"></td>
        </tr>
        <tr>
            <td>结束时间</td>
            <td><input type="date"  v-model="state.edate"></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" value="增加" @click="add"></td>
        </tr>
    </table>
</template>

<script setup lang="ts">
import {reactive,toRefs,onMounted} from 'vue';
import axios from 'axios';
import {useRouter} from 'vue-router';
const router = useRouter()

const data=reactive({
    state:{
  "roomPriceName": "",
  "roomPriceNum":'',
  "price": '',
  "roomTypeId": '1',
  "storeId": '2',
  "priceId": '',
  "breakfast": '',
  "people": '',
  "sdate": "",
  "edate": "",
  "state": 1,
  "isDeleted": false
    },
    roomtypelist:[],
    storelist:[]
})

const {state,roomtypelist,storelist}=toRefs(data);

onMounted(()=>{
    rtypedata(),
    sdata()
})

const rtypedata=()=>{
    axios({
        url:'/api/Room/SelRoomType',
        method:'get'
    })
    .then(res=>{
        console.log(res);
        roomtypelist.value=res.data;
    })
}

const sdata=()=>{
    axios({
        url:'/api/Room/SelMenStore',
        method:'get'
    })
    .then(res=>{
        console.log(res);
        storelist.value=res.data;
    })
}



const add=()=>{
    if(state.value.roomPriceName=='')
    {
        alert('房价名称不能为空');
        return;
    }
    if(state.value.roomPriceNum=='')
    {
        alert('房量数量不能为空');
        return;
    }
    if(isNaN(Number(state.value.roomPriceNum)))
    {
        alert('房量数量必须为数字');
        return;
    }
    if(state.value.price=='')
    {
        alert('设置价格不能为空');
        return;
    }
    if(isNaN(Number(state.value.price)))
    {
        alert('设置价格必须为数字');
        return;
    }
    if(state.value.roomTypeId=='')
    {
        alert('房型类型不能为空');
        return;
    }
    if(state.value.storeId=='')
    {
        alert('门店类型不能为空');
        return;
    }
    if(state.value.priceId=='')
    {
        alert('价格id不能为空');
        return;
    }
    if(isNaN(Number(state.value.priceId)))
    {
        alert('价格必须为数字');
        return;
    }
    if(state.value.breakfast=='')
    {
        alert('早晨数量不能为空');
        return;
    }
    if(isNaN(Number(state.value.breakfast)))
    {
        alert('早晨数量必须为数字');
        return;
    }
    if(state.value.people=='')
    {
        alert('人数数量不能为空');
        return;
    }
    if(isNaN(Number(state.value.people)))
    {
        alert('人数数量必须为数字');
        return;
    }
    if(state.value.sdate=='')
    {
        alert('开始时间不能为空');
        return;
    }
    if(state.value.edate=='')
    {
        alert('结束时间不能为空');
        return;
    }
    axios({
        url:'/api/Room/AddRoomPrice',
        method:'post',
        data:state.value
    })
    .then(res=>{
        console.log(res);
        if(res.data.data>0)
        {
            alert('新增房价计划成功');
            router.push('/');
        }
        else if(res.data.data==0)
        {
            alert('新增房价计划重复');
            return;
        }
        else
        {
            alert('新增房价计划失败');
            return;
        }
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>